/* Admin panel class
   By Michael Hsiao
   11/2/2007
*/

import java.awt.*;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.*;
import java.awt.event.*;

/* The administrator panel manages the user status.
 * The administrator can add users, edit users' info, and delete users.
 *
 */

public class AdminPanel extends JPanel implements ActionListener
{
	private UserTableModel userTableModel;
	private JTable userTable;
	private JScrollPane scrollPane;
	private JButton createUserButton;
	private JButton deleteUserButton;
	private JButton editUserButton;

    public AdminPanel()
    {
    	initUI();
    }

    private void initUI()
    {
    	TitledBorder border = BorderFactory.createTitledBorder("User Information");
		setBorder(border);

		setLayout(new GridBagLayout());

		GridBagConstraints cons = new GridBagConstraints();

		cons.fill = GridBagConstraints.HORIZONTAL;

		userTableModel = new UserTableModel();

		// Creates and displays the table onto the scrollpane
		userTable = new JTable(userTableModel);

		// Constraints for the scroll pane.
		cons.gridx = 0;
		cons.gridy = 0;
		cons.gridwidth = 4;
		cons.weightx = 1;
		cons.insets = new Insets(5, 5, 5, 5);
		add(new JScrollPane(userTable), cons);

		// Constraints for create user button
		cons = new GridBagConstraints();
		cons.fill = GridBagConstraints.HORIZONTAL;
		cons.gridx = 0;
		cons.gridy = 1;
		cons.gridwidth = 1;
		cons.insets = new Insets(5, 5, 5, 5);
		// Create user button
		createUserButton = new JButton("Create User");
		createUserButton.addActionListener(this);
		add(createUserButton,cons);

		// Constraints for edit user button
		cons = new GridBagConstraints();
		cons.fill = GridBagConstraints.HORIZONTAL;
		cons.gridx = 1;
		cons.gridy = 1;
		cons.gridwidth = 1;
		cons.insets = new Insets(5, 5, 5, 5);
		// Edit user button
		editUserButton = new JButton("Edit User");
		editUserButton.addActionListener(this);
		add(editUserButton,cons);

		// Constraints for delete user button
		cons = new GridBagConstraints();
		cons.fill = GridBagConstraints.HORIZONTAL;
		cons.gridx = 2;
		cons.gridy = 1;
		cons.gridwidth = 1;
		cons.insets = new Insets(5, 5, 5, 5);
		// Delete user button
		deleteUserButton = new JButton("Delete User");
		deleteUserButton.addActionListener(this);
		add(deleteUserButton,cons);

		userTableModel.updateUsers();
    }

    public void actionPerformed(ActionEvent e)
    {
    	if (e.getSource() == createUserButton) {
    		RegisterUserDialog dialog = new
    			RegisterUserDialog(null);

    		dialog.setVisible(true);

    		userTableModel.updateUsers();

    	} else if (e.getSource() == deleteUserButton) {

    		User user = userTableModel.getSelectedUser();
    		if (user != null) {
    			int result = JOptionPane.showConfirmDialog(null,
    					"Are you sure you want to delete this user?",
    					"Tower Control",
    					JOptionPane.YES_NO_OPTION);

    			if (result == JOptionPane.YES_OPTION) {
    				SessionManager.getInstance().deleteUser(user);
        			userTableModel.updateUsers();
    			}

    		} else {
    			JOptionPane.showMessageDialog(null,"You must select a user!");
    		}

    	}
    }


    // All table information goes in here.
    private class UserTableModel extends AbstractTableModel
    {
    	// Seven different columns:  User's name, positions, and online status
    	String[] columnNames = {"Name", "Supervisor", "Traffic Manager",
    				"Flight Data", "Ground Control", "Local Control",
    				"Helicopter Control", "Online"};

    	// Sample data.  Data entered is a two dimensional array of objects.
    	// Name is a String.  All the positions and online status are booleans.
    	/*
    	Object[][] data = {
    			{"Sample user", new Boolean(false), new Boolean(false), new Boolean(false),
    				new Boolean(false), new Boolean(false), new Boolean(false),
    				new Boolean(true)}
    			};
    	*/
    	Object[][] data;


    	// Essential functions

        public int getColumnCount()
        {   return columnNames.length;
        }

        public int getRowCount()
        {   return data.length;
        }

        public String getColumnName(int col)
        {   return columnNames[col];
        }

        // Returns the object at the indicated position
        public Object getValueAt(int row, int col)
        {   return data[row][col];
        }

        // Turns booleans into checkboxes.  Don't ask.  I don't know how it works myself.
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        // The table cannot be edited by the user, no matter what.
        public boolean isCellEditable(int row, int col)
        {  	return false;
        }

        // Edits the value at the indicated position
        public void setValueAt(Object value, int row, int col)
        {   data[row][col] = value;
        }

        public void updateUsers() {
        	setUsers(SessionManager.getInstance().getUsersArray());
        }

        public void setUsers(Object[] users) {
        	if (users.length == 0) {
        		data = null;
        		refreshData();
        		return;
        	}

        	data = new Object[users.length][8];

        	for (int u=0;u<users.length;u++) {
        		User user = (User)users[u];

        		data[u][0] = user;

        		for (int i=0;i<7;i++) data[u][i+1] = new Boolean(false);

        		if (user.hasPosition("Supervisor")) {
        			data[u][1] = new Boolean(true);
        		}
        		if (user.hasPosition("Traffic Manager")) {
        			data[u][2] = new Boolean(true);
        		}
        		if (user.hasPosition("Flight Data/Clearance Delivery")) {
        			data[u][3] = new Boolean(true);
        		}
        		if (user.hasPosition("Ground Control")) {
        			data[u][4] = new Boolean(true);
        		}
        		if (user.hasPosition("Local Control")) {
        			data[u][5] = new Boolean(true);
        		}
        		if (user.hasPosition("Helicopter Control")) {
        			data[u][6] = new Boolean(true);
        		}

        	}
        	refreshData();
        }

        public User getSelectedUser() {
    		int row = userTable.getSelectedRow();
        	if (row < 0 || row >= data.length) {
        		return null;
        	}
        	return (User)data[row][0];
        }

        public void refreshData() {
        	this.fireTableDataChanged();
        }
    }

}
